define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/appoint/user/index',
                    detail_url: 'xiluhouse/appoint/user/detail',
                    edit_url: 'xiluhouse/appoint/user/edit',
                    del_url: 'xiluhouse/appoint/user/del',
                    multi_url: 'xiluhouse/appoint/user/multi',
                    dragsort_url:'',
                    user_url: 'xiluhouse/user/user/index',
                    table: 'user',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='broker_user_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.user_url).data("primaryKey", "id").data("field", "mobile").data("params", function (obj) {
                    return {custom: {'userprofile.is_broker': 2}};
                });
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: __('Id'), sortable: true},
                        {field: 'userdata.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'userdata.mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'userdata.avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        { field: 'source_type', title: __('source_type'), searchList: { '1': __('source_type 1'), '2': __('source_type 2'), '3': __('source_type 3') }, formatter: Table.api.formatter.normal },
                        {field: 'user_type', title: __('user_type'), searchList: {'0': __('user_type 0'),'1': __('user_type 1'), '2': __('user_type 2'), '3': __('user_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'max_house_state', title: __('max_house_state'), searchList: {"0":__('House_state 0'),"1":__('House_state 1'),"2":__('House_state 2'),"3":__('House_state 3'),"4":__('House_state 4'),"5":__('House_state 5'),"6":__('House_state 6')}, formatter: Table.api.formatter.normal},
                        { field: 'house_state', title: __('House_state'), searchList: { "0": __('House_state 0'), "1": __('House_state 1'), "2": __('House_state 2'), "3": __('House_state 3'), "4": __('House_state 4'), "5": __('House_state 5'), "6": __('House_state 6') }, formatter: Table.api.formatter.normal },
                        {field: 'broker_user_id', title: __('Broker_user_id'),formatter: function (value, row, index) {
                            if(row.broker_user_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.user_url+ "?id=" + row.broker_user_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.broker_user_id +'】'+row.broker.nickname+'<br/>'+row.broker.mobile+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        
                        {field: 'weigh', title: __('Weigh'),sortable:true},
                        {field: 'userdata.status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [], formatter: Table.api.formatter.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var buttons = that.buttons;
                                buttons[0]={
                                    name: 'detail', 
                                    text: '', 
                                    title: '查看详情',
                                    icon: 'fa fa-eye',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'xiluhouse/appoint/user/detail?ids='+row.user_id
                                };
                                buttons[1]={
                                    name: 'appointlog', 
                                    text: '预约订单', 
                                    title: '预约订单',
                                    icon: 'fa fa-flash',
                                    classname: 'btn btn-xs btn-warning bg-blue btn-dialog',
                                    url: 'xiluhouse/appoint/appointlog/index?user_id='+row.user_id
                                };
                                that.buttons = buttons;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});